﻿using System;
using System.Web;
using System.Text;
using Axis.Dao.Research;
using System.Xml;
using Axis.Web.Administrator.Global;
using Axis.BusinessObject.Research;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using Axis.BusinessObject.Core;

namespace Axis.Web.Views.Research
{
    public class CQuestionUI : System.Web.UI.Page
    {
        public string GetQuestionDetail(string ProjectID)
        {
            var MEMBER_ID = 0;
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
            {
                member = Session["MEMBERINFO"] as CMemberList;
                MEMBER_ID = member.ID;
            }

            StringBuilder ret = new StringBuilder();
            StringBuilder result = new StringBuilder();
            CResearchDao cResearchDao = new CResearchDao();
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("Action", "GET");
            root.SetAttribute("SecrectToken", member.SecrectToken);
            root.SetAttribute("PageIndex", "1");
            root.SetAttribute("RowsPerPage", "999");
            root.SetAttribute("ProjectID", ProjectID);
            IList<CQuestionaire> list = cResearchDao.GetQuestionaire(root.OuterXml);
            string QuesRow = "";
            string QuesCol = "";
            int QuesKey = 0;
            int QuesType = 0;
            if (list.Count > 0)
            {
                int QuesID = list[0].ID;
                QuesRow = GetQuestionareList(QuesID, ProjectID, out QuesKey);
                QuesCol = GetResponseListByQuestionnaire(QuesKey);
                //Phú mày trả về cho tao type chỗ này. 1: single | 0: Multi
                QuesType = (new ResearchUI()).GetProjectType(string.Format("<InputValue ID=\"{0}\" Context=\"ByProjectID\" />", ProjectID));
            }
            result.AppendFormat("{{\"QuesKey\":{0},", QuesKey);
            result.AppendFormat("\"QuesType\":{0},", QuesType);
            if (QuesRow.Length > 0)
                result.AppendFormat("\"QuesRow\":{0},", QuesRow);
            else
                result.Append("\"QuesRow\":[],");
            if (QuesCol.Length > 0)
                result.AppendFormat("\"QuesCol\":{0}", QuesCol);
            else
                result.Append("\"QuesCol\":[]");
            result.Append("}\n");
            return result.ToString();
        }

        public string GetQuestionareList(int QuestionnaireID, string ProjectID, out int QuesKey)
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            CResearchDao core = new CResearchDao();
            QuesKey = 0;
            IList<CQuestionaire> Map = core.GetQuestionaire(string.Format("<InputValue UserID='{0}' ID='{1}' ProjectID='{2}'/>", member.ID.ToString(), QuestionnaireID, ProjectID));
            if (Map.Count > 0)
                QuesKey = Map[0].ID;
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(Map);
        }

        public string GetResponseListByQuestionnaire(int QuestionnaireID)
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            CResearchDao core = new CResearchDao();
            IList<CResponseOption> Map = core.GetResponseOption(string.Format("<InputValue UserID=\"{0}\" QuestionnaireID=\"{1}\" />", member.ID.ToString(), QuestionnaireID));
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(Map);
        }
    }
}